'use strict';
const md5 = require('md5');
const Controller = require('egg').Controller;

class LiveController extends Controller {
  async addlive() {
    let user_id = this.ctx.authUser.id
    this.ctx.validate({
      type: {
        type: "string", required: true, desc: "类型"
      },

      title: {
        type: "string",
        required: true,
        desc: "直播间标题"
      },
      cover: {
        type: "string",
        required: false,
        desc: "直播间封面"
      }
    })
    let key = this.ctx.randomString(20);
    let res = await this.app.model.Live.create({
      title: this.ctx.request.body.title,
      cover: this.ctx.request.body.cover,
      user_id: user_id,
      key: key
    })
    let sign = this.sign(key)
    let obj = {
      res: res,
      sign: sign
    }

    this.ctx.apiSuccess(obj, "创建成功", 200)
  }
  async editlive(){
    console.log(this.ctx.authUser,"this.ctx.authUser")
    let user_id = this.ctx.authUser.id
    console.log(user_id,"user_idf")
    this.ctx.validate({
      id: {
        type: "string", required: true, desc: "id"
      },
      type: {
        type: "string", required: true, desc: "类型"
      },
      status: {
        type: "int",
        required: true,
        desc: "状态"
      },
      
    })
    const res=await this.app.model.Live.findOne({
      where:{
        id:this.ctx.request.body.id,
        user_id:user_id
      
      }
      })
    console.log(res,"res")
    if(res.status == 3){
      return this.ctx.apiFail("该直播间已结束")
    }
    if(!res){
      return this.ctx.apiFail("该直播间不存在")
    }
    res.status=this.ctx.request.body.status;
    res.save()
    this.ctx.apiSuccess(res,"修改成功",200)
  }
  async list() {

    let page = this.ctx.request.body.page ? parseInt(this.ctx.request.body.page) : 1
    let limit = parseInt(this.ctx.request.body.limit);
    console.log(page, limit)
    let offset = (page - 1) * limit;
    this.ctx.validate({
      status: {
        type: "int",
        required: false,
        defValue: "",
        desc: "状态",
      },

    })
    const res = await this.app.model.Live.findAll({
      where: this.ctx.request.body.status !== '' ? {
        status: this.ctx.request.body.status
      } : null,
      offset,
      limit
    })


    let count = await this.app.model.Live.count({
      where: this.ctx.request.body.status !== '' ? {
        status: this.ctx.request.body.status
      } : null,
    });
    console.log(count, "count")
    let obj = {
      data: res,
      count: count
    }
    this.ctx.apiSuccess(
      obj, "获取成功", 200
    )
  }
  async list1() {

    let page = this.ctx.request.body.page ? parseInt(this.ctx.request.body.page) : 1
    let limit = parseInt(this.ctx.request.body.limit);
    console.log(page, limit)
    let offset = (page - 1) * limit;
    this.ctx.validate({
      type: {
        type: "string", required: true, desc: "类型"
      },

    })
    const res = await this.app.model.Live.findAll({
     
      // offset,
      // limit
    })


    let count = await this.app.model.Live.count({
     
    });
    console.log(count, "count")
    let obj = {
      data: res,
      count: count
    }
    this.ctx.apiSuccess(
      obj, "获取成功", 200
    )
  }
  async liveinfo(){
    this.ctx.validate({
      id: {
        type: "int",
        required: true,
        defValue: "",
        desc: "id",
      },
      type: {
        type: "string", required: true, desc: "类型"
      },

    })
    const res=await this.app.model.Live.findOne({
      where:{
        id:this.ctx.request.body.id
      },
      include:[{
        model:this.app.model.User,
        attributes:["id","username","avatar"]
      }]
    })
    if(!res){
      this.ctx.apiFail("该直播不存在")
    }
    let sign = this.sign(res.key)
    let obj = {
      res: res,
      sign: sign
    }

    this.ctx.apiSuccess(obj, "获取成功", 200)
  }
  sign(key) {
    const secret = this.app.config.mediaServer.auth.secret;
    const expire = parseInt(Date.now() + 1000000000 / 1000);
    const hashValue = md5(`/catlive/${key}-${expire}-${secret}`)
    return `${expire}-${hashValue}`
  }
  formatTime(time) {
    let d = new Date(time);
    const Month = (d.getMonth() + 1) >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1)
    const Day = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate()
    const h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours()
    const m = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes()
    const s = d.getSeconds() >= 10 ? d.getSeconds() : '0' + d.getSeconds()
    console.log(d.getFullYear() + '-' + Month + '-' + Day + ' ' + h + ':' + m + ':' + s)
    return d.getFullYear() + '-' + Month + '-' + Day + ' ' + h + ':' + m + ':' + s;

  }
}

module.exports = LiveController;
